package syn;

public class MaipiaoTest {
	public static void main(String[] args) throws InterruptedException {
		Account account = new Account(300, "郑州银行");
		Quqianji t1 = new Quqianji(account, 50, "   一号   ");
		Quqianji t2 = new Quqianji(account, 100, "  二号   ");
		Quqianji t3 = new Quqianji(account, 100, "  三号   ");
		Quqianji t4 = new Quqianji(account, 250, "  四号   ");

		t1.start();
		t2.start();
		t3.start();
		t4.start();

	}
}

//创建一张卡
class Account {
	int money;
	String kaohao;

	public Account(int money, String kaohao) {
		this.money = money;
		this.kaohao = kaohao;
	}
}

//模拟取钱
class Quqianji extends Thread {
	//放卡取钱
	Account account;
	//取钱人
	String name;


	//取啦多少钱
	int quqian;
	//手里有
	int nowmoney;

	public Quqianji() {
	}

	public Quqianji(Account account, int quqian, String name) {
		this.account = account;
		this.quqian = quqian;
		this.name = name;
	}

	//进行取钱
	@Override
	public void run() {
		try {
			getmonoy();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}

	public void getmonoy() throws InterruptedException {
		synchronized (account) {
			if (account.money - quqian < 0) {
				System.out.println("余额不足为:" + account.money + this.name + "取" + quqian + "  取不到钱");
				return;
			}
			//Thread.sleep(100);
			//更新卡内余额
			account.money = account.money - quqian;
			//你手里的钱
			nowmoney = nowmoney + quqian;
			System.out.println(this.name + "取啦" + nowmoney + "元钱");
			System.out.println("卡内余额还剩" + account.money);
		}
	}
}